package com.zecan.greedAlgorithm;

import java.util.Arrays;

/**
 * \* Created with IntelliJ IDEA.
 * \* User: lenovo
 * \* Date: 2021/10/13
 * \* Time: 11:35
 * \* Description:
 * \
 */
public class Assignment {

    public static void main(String[] args) {
        int[] childs = {2,1,3,5,8,4};
        int[] cookies = {2,1,3,8,4};
        int assignment = getAssignment(childs, cookies);
        System.out.println(assignment);
    }
    /**
     *
     * @param childs 当前孩子的数组
     * @param cookies 饼干的数组
     * @return 返回可以吃饱的孩子的数量
     * @author
     * @creed: Talk is cheap,show me the code
     * @date  2021年10月13日11:37:43
     */
    static int getAssignment(int[] childs, int [] cookies) {
        // 先将饼干大小和孩子的饥饿度排序
        Arrays.sort(childs);
        Arrays.sort(cookies);
        int chid = 0; // 标记可以吃饱的孩子数量
        int cookie = 0;
        int ckIndex = 0; // 指向饼干大小的指针
        int cIndex = 0; // 指向孩子饥饿度的指针
        while(cIndex < childs.length && ckIndex < cookies.length) {
            if(cookies[cookie] >= childs[chid]) {
                chid++;
                cookie++;
                cIndex++;
                ckIndex++;
            }
        }
        return chid;
    }
}
